package com.clementheliou.cheliou.dal.model.mark.builder;

import org.springframework.util.Assert;

import com.clementheliou.cheliou.dal.dao.camera.brand.CameraBrandDAO;
import com.clementheliou.cheliou.dal.dao.camera.model.CameraModelDAO;
import com.clementheliou.cheliou.dal.dao.city.CityDAO;
import com.clementheliou.cheliou.dal.dao.comment.CommentDAO;
import com.clementheliou.cheliou.dal.dao.country.CountryDAO;
import com.clementheliou.cheliou.dal.dao.mark.MarkDAO;
import com.clementheliou.cheliou.dal.dao.member.MemberDAO;
import com.clementheliou.cheliou.dal.dao.photo.PhotoDAO;
import com.clementheliou.cheliou.dal.model.mark.Mark;
import com.clementheliou.cheliou.dal.model.member.Member;
import com.clementheliou.cheliou.dal.model.member.builder.MemberBuilder;
import com.clementheliou.cheliou.dal.model.photo.Photo;
import com.clementheliou.cheliou.dal.model.photo.builder.PhotoBuilder;
import com.clementheliou.cheliou.util.builder.ObjectBuilder;
import com.clementheliou.cheliou.util.time.TimeUtils;

/**
 * Builder used to get {@link Mark} instances for test purpose. It must only be used through one of
 * its static factory methods.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link Mark};
 * @see {@link MarkBuilder}.
 * @since 1.0
 */
public class MarkBuilder extends ObjectBuilder<Mark> {

	/**
	 * The {@link CameraBrandDAO} to be transmitted to the {@link PhotoBuilder} if the instance must
	 * be persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link PhotoBuilder} .
	 * @since 1.0
	 */
	private final CameraBrandDAO cameraBrandDAO;

	/**
	 * The {@link CameraModelDAO} to be transmitted to the {@link PhotoBuilder} if the instance must
	 * be persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link PhotoBuilder} .
	 * @since 1.0
	 */
	private final CameraModelDAO cameraModelDAO;

	/**
	 * The {@link CityDAO} to be transmitted to the {@link PhotoBuilder} if the instance must be
	 * persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link PhotoBuilder} .
	 * @since 1.0
	 */
	private final CityDAO cityDAO;

	/**
	 * The {@link MarkDAO} to be used to persist the instance if needed.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private final MarkDAO markDAO;

	/**
	 * The {@link CountryDAO} to be transmitted to the {@link PhotoBuilder} if the instance must be
	 * persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link PhotoBuilder} .
	 * @since 1.0
	 */
	private final CountryDAO countryDAO;

	/**
	 * The {@link MemberDAO} to be transmitted to the {@link MemberBuilder} if the instance must be
	 * persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link MemberBuilder}.
	 * @since 1.0
	 */
	private final MemberDAO memberDAO;

	/**
	 * The {@link PhotoDAO} to be transmitted to the {@link PhotoBuilder} if the instance must be
	 * persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link PhotoBuilder} .
	 * @since 1.0
	 */
	private final PhotoDAO photoDAO;

	/**
	 * Private constructor from the DAO to be used.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param cameraBrandDAO the {@link CameraBrandDAO} to be used.
	 * @param cameraModelDAO the {@link CameraModelDAO} to be used.
	 * @param cityDAO the {@link CityDAO} to be used.
	 * @param countryDAO the {@link CommentDAO} to be used.
	 * @param markDAO the {@link MarkDAO} to be used.
	 * @param memberDAO the {@link MemberDAO} to be used.
	 * @param photoDAO the {@link PhotoDAO} to be used.
	 * @since 1.0
	 */
	private MarkBuilder(CameraBrandDAO cameraBrandDAO, CameraModelDAO cameraModelDAO, CityDAO cityDAO, CountryDAO countryDAO, MarkDAO markDAO, MemberDAO memberDAO, PhotoDAO photoDAO) {
		this.cameraBrandDAO = cameraBrandDAO;
		this.cameraModelDAO = cameraModelDAO;
		this.cityDAO = cityDAO;
		this.countryDAO = countryDAO;
		this.markDAO = markDAO;
		this.memberDAO = memberDAO;
		this.photoDAO = photoDAO;
	}

	/**
	 * Private default constructor.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private MarkBuilder() {
		this(null, null, null, null, null, null, null);
	}

	/**
	 * Static factory method used to get a builder instance for a {@link Mark} object that must be
	 * persistent. It's mostly used on data access layer's methods tests.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param cameraBrandDAO the {@link CameraBrandDAO} to be used.
	 * @param cameraModelDAO the {@link CameraModelDAO} to be used.
	 * @param cityDAO the {@link CityDAO} to be used.
	 * @param countryDAO the {@link CommentDAO} to be used.
	 * @param markDAO the {@link MarkDAO} to be used.
	 * @param memberDAO the {@link MemberDAO} to be used.
	 * @param photoDAO the {@link PhotoDAO} to be used.
	 * @return the builder instance to be used.
	 * @since 1.0
	 */
	public static MarkBuilder with(CameraBrandDAO cameraBrandDAO, CameraModelDAO cameraModelDAO, CityDAO cityDAO, CountryDAO countryDAO, MarkDAO markDAO, MemberDAO memberDAO, PhotoDAO photoDAO) {
		return new MarkBuilder(cameraBrandDAO, cameraModelDAO, cityDAO, countryDAO, markDAO, memberDAO, photoDAO);
	}

	/**
	 * Static factory method used to get a builder instance for a {@link Mark} object that must not
	 * be persistent. It's mostly used in tests not having a real data access.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the builder instance to be used.
	 * @since 1.0
	 */
	public static MarkBuilder withNoDependencies() {
		return new MarkBuilder();
	}

	/**
	 * Creates a default {@link Mark} instance, ready to be persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the builder instance after this action.
	 * @since 1.0
	 */
	public MarkBuilder createDefaultMark() {
		setInstance(createInstance());
		return this;
	}

	/**
	 * Creates a default {@link Mark} mock that should never be persisted.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the builder instance after this action.
	 * @since 1.0
	 */
	public MarkBuilder createDefaultMarkMock() {
		setInstance(createInstance(new Member(), new Photo()));
		return this;
	}

	/**
	 * Persists the built {@link Mark} instance then flush the current session.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the builder instance after this action.
	 * @since 1.0
	 */
	public MarkBuilder persistAndFlush() {
		Assert.notNull(markDAO, "markDAO must not be null at this time");

		markDAO.saveEntity(getObject());
		markDAO.flushSession();

		return this;
	}

	/**
	 * Creates a {@link Mark} instance with default values, creating the photo it applies to and the
	 * author giving it. Created data have a functional sense.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @return the built instance.
	 * @since 1.0
	 */
	private Mark createInstance() {
		Assert.notNull(cameraBrandDAO, "cameraBrandDAO must not be null at this time");
		Assert.notNull(cameraModelDAO, "cameraModelDAO must not be null at this time");
		Assert.notNull(cityDAO, "cityDAO must not be null at this time");
		Assert.notNull(countryDAO, "countryDAO must not be null at this time");
		Assert.notNull(memberDAO, "memberDAO must not be null at this time");
		Assert.notNull(photoDAO, "photoDAO must not be null at this time");

		final Photo markedPhoto = PhotoBuilder.with(cameraBrandDAO, cameraModelDAO, cityDAO, countryDAO, photoDAO).createDefaultPhoto().persistAndFlush().getObject();
		return createInstance(MemberBuilder.with(memberDAO).createDefaultMember().persistAndFlush().getObject(), markedPhoto);
	}

	/**
	 * Creates a {@link Mark} instance with default values for the given photo and author. Created
	 * data have a functional sense.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param author the member giving the created mark.
	 * @param markedPhoto the photo the created mark will concern.
	 * @return the built instance.
	 * @since 1.0
	 */
	private Mark createInstance(final Member author, final Photo markedPhoto) {
		final Mark instance = new Mark();

		instance.setAdditionDate(TimeUtils.now());
		instance.setAuthor(author);
		instance.setMarkedPhoto(markedPhoto);
		instance.setRank(2);

		return instance;
	}
}
